<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header
      >Header
      <div><el-button type="danger" plain @click="tuichu">退出</el-button></div>
    </el-header>
    <!-- 页面主题区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle_button" @click="toggleHandler">|||</div>
        <!-- unique-opened设置只打开一个栏目 -->
        <!-- collapse左侧展开与否 -->
        <!-- router设置路由模式 -->
        <!-- default-active路由高亮模式 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="rgb(64, 158, 255)"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="$route.path"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id.toString()"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }} </span>
            </template>
            <!-- 这是二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
            >
              <!-- 一级菜单的模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }} </span>
              </template></el-menu-item
            >
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  export default {
    data() {
      return {
        menuList: [],
        iconObj: {
          125: 'iconfont icon-user',
          103: 'iconfont icon-tijikongjian',
          101: 'iconfont icon-shangpin',
          102: 'iconfont icon-danju',
          145: 'iconfont icon-baobiao',
        },
        isCollapse: false,
      }
    },
    methods: {
      tuichu() {
        localStorage.clear()
        this.$router.replace('/login')
      },
      async getList() {
        const { data: res } = await this.$http.get('aside.php')
        this.menuList = JSON.parse(res).slice(1)
      },
      toggleHandler() {
        this.isCollapse = !this.isCollapse
      },
    },
    created() {
      this.getList()
    },
  }
</script>

<style lang='less' scoped>
  .el-container {
    font-size: 20px;
    color: white;
    height: 100vh;
    .el-header {
      background: #373d41;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 45px;
    }
    .el-aside {
      background: #333744;
      .el-menu {
        border-right: none;
      }
    }
    .el-main {
      color: black;
      background: #eaedf1;
    }
  }
  .iconfont {
    margin-right: 10px;
  }
  .toggle_button {
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
</style>